﻿//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AnalysisServices;

namespace SMK_SSAS
{
    /// <summary>
    /// 
    /// </summary>
    public class AMO_Database
    {
        private Microsoft.AnalysisServices.Database amodb = null;
        private AMO_Server lServer = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Server"></param>
        /// <param name="DatabaseID"></param>
        public void SetAMODatabase(AMO_Server Server, string DatabaseID)
        {
            if (Server != null && Server.Isconnected())
            {
                lServer = Server;
                if (Server.amoserver().Databases.Contains(DatabaseID))
                {
                    amodb = Server.amoserver().Databases.Find(DatabaseID);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public AMO_Server AMOServer()
        {
            if (lServer != null && lServer.Isconnected())
            {

                return lServer;

            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Microsoft.AnalysisServices.Database amodatabase()
        {
            if (amodb != null)
            {

                return amodb;

            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasAccounts()
        {
            if (amodb != null)
            {
                if (amodb.Accounts.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasAssemblies()
        {
            if (amodb != null)
            {
                if (amodb.Assemblies.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasCubes()
        {
            if (amodb != null)
            {
                if (amodb.Cubes.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasDatabasePermissions()
        {
            if (amodb != null)
            {
                if (amodb.DatabasePermissions.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasDataSources()
        {
            if (amodb != null)
            {
                if (amodb.DataSources.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasDataSourceViews()
        {
            if (amodb != null)
            {
                if (amodb.DataSourceViews.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasDimensions()
        {
            if (amodb != null)
            {
                if (amodb.Dimensions.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasMiningStructures()
        {
            if (amodb != null)
            {
                if (amodb.MiningStructures.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasRoles()
        {
            if (amodb != null)
            {
                if (amodb.Roles.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasTranslations()
        {
            if (amodb != null)
            {
                if (amodb.Translations.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Roles()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Role db in amodb.Roles)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Accounts()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Account db in amodb.Accounts)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.AccountType;
                    amok.Name = db.AccountType;
                    result.Add(amodb.Name + "\\" + db.AccountType, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Assemblies()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Assembly db in amodb.Assemblies)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Cubes()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Cube db in amodb.Cubes)
                {
                    AMO_Key amok = new AMO_Key();
                    AMO_Database_Cube amodbc = new AMO_Database_Cube();
                    amodbc.SetAMODatabaseCube(this, db.ID);
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    amok.LevelObject = amodbc;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DataSources()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.DataSource db in amodb.DataSources)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DataSourceviews()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.DataSourceView db in amodb.DataSourceViews)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DatabasePermissions()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.DatabasePermission db in amodb.DatabasePermissions)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DatabaseDimensions()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Dimension db in amodb.Dimensions)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DatabaseMiningStructures()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.MiningStructure db in amodb.MiningStructures)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.ID;
                    amok.Name = db.Name;
                    result.Add(amodb.Name + "\\" + db.ID, amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> DatabaseTranslations()
        {
            if (amodb != null)
            {
                SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
                foreach (Microsoft.AnalysisServices.Translation db in amodb.Translations)
                {
                    AMO_Key amok = new AMO_Key();
                    amok.ID = db.Language.ToString();
                    amok.Name = db.Caption;
                    result.Add(amodb.Name + "\\" + db.Language.ToString(), amok);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
    }
}
